<?php

/**
 * This is the model class for table "store".
 *
 * The followings are the available columns in table 'store':
 * @property integer $id
 * @property string $name
 * @property string $logo_url
 * @property string $content
 *
 * The followings are the available model relations:
 * @property Product[] $products
 */
class Store extends CActiveRecord
{
	public $termAcepted = false;
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Store the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'store';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('name, new_visit_price', 'required'),
			array('name', 'length', 'max'=>255),
			array('content', 'safe'),
			array('chat_code', 'safe'),
			array('logo_url', 'file', 'types'=>'jpg, png, gif', 'allowEmpty' => true),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, name, content', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'products' => array(self::HAS_MANY, 'product', 'store_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'name' => 'Nombre',
			'logo_url' => 'Logo',
			'content' => 'Descripción',
			'termAcepted' => 'Acépto los ',
			'new_visit_price' => 'Precio por visita',
			'chat_code' => 'Codigo GTalk',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('name',$this->name,true);
		$criteria->compare('logo_url',$this->logo_url,true);
		$criteria->compare('content',$this->content,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
	
	public function allStores(){
		return Yii::app()->db->createCommand('
			SELECT
				s.id, s.name, s.logo_url, p.id as pid, 
				p.name as pname, p.price_from, i.url_110
			FROM
				store s
				JOIN product p ON s.product_id = p.id
				JOIN media i ON p.media_id = i.id
			WHERE
				s.balance IS NOT NULL
			ORDER BY
				s.visit_price DESC
		')->queryAll();
	}
}